<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Node(value) {
            this.value = value;
            this.left = null;
            this.right = null;
            this.next = null;
        }
        var a = new Node(1)
        var b = new Node(null)
        var c = new Node(2)
        var d = new Node(3)
        var e = new Node(4)
        var f = new Node(5)
        var g = new Node(6)
        a.left = b
        a.right = c
        b.left = d
        b.right = e
        c.left = f
        c.right = g
        var maxDepth = function (root) {
            //二叉树的层序遍历
            let res = [], queue = [];
            queue.push(root);
            if (root === null) {
                return res;
            }
            let depth = 0
            while (queue.length !== 0) {
                // 记录当前层级节点数 length 是上一次for循环 push进入queue里面的节点的个数
                let length = queue.length;
                //存放每一层的节点 每次都要更新
                let curLevel = [];
                depth++
                for (let i = 0; i < length; i++) {
                    // 在length的限制下 不会把下一层的也打印出来 length值必须固定
                    let node = queue.shift();
                    curLevel.push(node.val);
                    // 存放当前层下一层的节点
                    // 同时不会改变length的值的情况
                    // 左节点和右节点 放到queue
                    // 先左 再 push 右 到时候shift从顶部拿，就是从左到右打印
                    node.left && queue.push(node.left);
                    node.right && queue.push(node.right);
                }
                //把每一层的结果放到结果数组
                //每一层的结果都是一个数组
                res.push(curLevel);
            }
            return depth;
        };
        console.log(maxDepth(a));
    </script>
</body>

</html>